package com.team.cluedo.app;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.team.cluedo.Game;
import com.team.cluedo.R;
import com.team.cluedo.app.utils.CluedoMenu;
import com.team.cluedo.app.waitscreens.WaitForYourTurn;

public class CreateGame extends Activity implements OnClickListener{
    
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		boolean res = CluedoMenu.onCreateOptionsMenu(menu, this);
		menu.getItem(CluedoMenu.MENU_CURRENT_GAME).setEnabled(false);
		menu.getItem(CluedoMenu.MENU_MY_CARDS).setEnabled(false);
		return res;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return CluedoMenu.onOptionsItemSelected(item, this);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	    setContentView(R.layout.creategame);
	    Button validateButton = (Button) findViewById(R.id.ButtonValidate);
	    validateButton.setOnClickListener(this);
	}
	public void onClick(View view) {
		Intent defineIntent;
		switch (view.getId()) {
		case R.id.ButtonValidate:
			
			//recup value of R.id.textCreatedGame and text nbPlayer TODO
			TextView nameTV = (TextView) findViewById(R.id.EditTextGameName);
			TextView nbTV = (TextView) findViewById(R.id.EditTextPlayerNumber);
			String name = nameTV.getText()+""; //
			int nbPlayer = Integer.parseInt(nbTV.getText()+""); //
			
			if(Game.cluedo.waitForCreateGame(name,nbPlayer)){
				if(Game.cluedo.waitJoinGame(name)){
					defineIntent = new Intent(this,WaitForYourTurn.class);
		     		Game.cluedo.activity.startActivity(defineIntent);
		     		this.finish();
				}else{
					//if join not possible show Error message TODO
					defineIntent = new Intent(this,Game.class);
		     		this.startActivity(defineIntent);
		     		this.finish();
				}
				
			}else{
				//if creation not possible show Error message TODO 
				defineIntent = new Intent(this,Game.class);
	     		this.startActivity(defineIntent);
	     		this.finish();
			}
			
		break;
		default: break;
		}
	}
}